package SmartGrid;


import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;

import Plants.PowerPlant;
import Tasks.ManipulateDemand;

/**
 * Monitors supply and demand of the energy market
 * @author jeffrey
 *
 */
public class EnergyMarket {

	public static AtomicInteger demand = new AtomicInteger();  //total demand - what a dumb grid would know
	public static AtomicInteger supply = new AtomicInteger();  //total supply - what a dumb grid would know
	
	/**
	 * Initializes the market and assigns 1 consumer to each power plant
	 * Spawns a separate thread to maintain the market and manipulate demand over time
	 */
	public static void init()
	{
		Iterator<PowerPlant> plants = SmartGrid.allPlants.values().iterator();
		while(plants.hasNext())
		{
			PowerPlant plant = plants.next();
			ConsumerEnergyMeter consumer = new ConsumerEnergyMeter();
			
			plant.addConsumer(consumer);
			consumer.setLocation(plant);
			
		}
		demand.set(SmartGrid.allPlants.size() * Tools.Options.INIT_CONSUMER_DEMAND);
		ManipulateDemand();
	}
	
	private static void ManipulateDemand()
	{
		ManipulateDemand demand = new ManipulateDemand();
		ExecutionEngine.addRecurringTask(demand, Tools.Options.MANIPULATE_DEMAND_DELAY);
	}
	
}
